//
//  RadioStationViewController.m
//  LuckyRadio
//
//  Created by Mr、J on 15/11/19.
//  Copyright © 2015年 chenjun. All rights reserved.
//

#import "RadioStationViewController.h"
#import "CustomerNetworking.h"
#import "RadioModel.h"
#import "RadioStationModel.h"
#import "HeaderCollectionReusableView.h"
#import "RadioStationCollectionViewCell.h"
#import <MBProgressHUD.h>

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#define WIDTH_PRO(X) (X)*(SCREEN_WIDTH/375)

#define HEIGHT_PRO(Y) (Y)*(SCREEN_HEIGHT/667)
@interface RadioStationViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, MBProgressHUDDelegate>

@property (nonatomic, retain) UICollectionView *collectionView;

@property (nonatomic, retain) NSMutableArray *modelArray;

@property (nonatomic, retain) MBProgressHUD *HUD;

@end

@implementation RadioStationViewController
- (void)dealloc
{
    [_collectionView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.modelArray = [NSMutableArray array];
    
    
    [self createSubViews];
    [self loadData];
    
    
}

-(void)createSubViews
{
    UICollectionViewFlowLayout *collectionViewFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    collectionViewFlowLayout.itemSize = CGSizeMake((SCREEN_WIDTH - WIDTH_PRO(30)) / 2, SCREEN_WIDTH / 4);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - HEIGHT_PRO(60)) collectionViewLayout:collectionViewFlowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
    [_collectionView release];
    [collectionViewFlowLayout release];
    
    [self.collectionView registerClass:[RadioStationCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.collectionView registerClass:[HeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
   }

-(void)loadData
{
    self.HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];
    self.HUD.delegate = self;
    
    //常用的设置
    //小矩形的背景色
   // self.HUD.color = [UIColor clearColor];//这儿表示无背景
    //显示的文字
    self.HUD.labelText = @"正在进行加载~";
    //是否有庶罩
   // self.HUD.dimBackground = YES;
   // [self.HUD hide:YES afterDelay:2];

    
    
    [CustomerNetworking customerWithURL:@"http://fm.api.ttpod.com/radiolist?image_type=240_200&app=ttpod&v=v8.1.1.2015110616&user_id=0&mid=iPhone5S&f=f234&s=s310&imsi=&hid=&splus=8.4.1&active=1&net=2&openudid=e97c1f17f916efa173065a10152c7506e65b60f7&idfa=5FBD2BF5-1484-4C9B-A138-4D9CE22EADB5&utdid=VkKezJUW5SwDAGga0yzrZnyO&alf=700606&bundle_id=com.ttpod.music&latitude=38.89679413984461&longtitude=121.5286724114796" andParamer:nil andBlock:^(id response) {
        
        for (NSDictionary *dic in [response objectForKey:@"data"]) {
            RadioModel *radioModel = [RadioModel modelWithDictionary:dic];
            [self.modelArray addObject:radioModel];
        }
        [self.collectionView reloadData];
        [self.HUD hide:YES];
    }];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    RadioModel *radioModel = _modelArray[section];
    
    return radioModel.data.count;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return _modelArray.count;
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, WIDTH_PRO(10), 0, WIDTH_PRO(10));
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(SCREEN_WIDTH, HEIGHT_PRO(40));
}

-(HeaderCollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        RadioModel *radioModel = _modelArray[indexPath.section];
        HeaderCollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerView" forIndexPath:indexPath];
        headView.nameLabel.text = radioModel.tag_type_name;
            return headView;
    }else{
        return nil;
    }
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    RadioModel *radioModel = _modelArray[indexPath.section];
    RadioStationModel *radioStationModel = [RadioStationModel modelWithDictionary:radioModel.data[indexPath.item]];
    
    RadioStationCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.radioStationModel = radioStationModel;
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
   // RadioStationCollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    
    RadioModel *radioModel = _modelArray[indexPath.section];
    RadioStationModel *radioStationModel = [RadioStationModel modelWithDictionary:radioModel.data[indexPath.item]];
    radioStationModel.isCompare = YES;
    [collectionView reloadData];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
